package com.winway.sixbook.ui;

import com.winway.sixbook.HomeActivity;
import com.winway.sixbook.R;
import com.winway.sixbook.custom.CloseActivityClass;
import com.winway.sixbook.custom.CustomSca;
import com.winway.sixbook.service.CounterDataManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;


public class CounterListActivity extends Activity {
	
	private static final String TAG = "CounterListActivity";
	ListView listView = null;
	private CounterDataManager counterDataManager;
	
//	private ImageButton top_tbn_fanhui;
//	private Button top_tbn_xinzeng;
//	private TextView title;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.counterlist);
		CloseActivityClass.activityList.add(this);
		listView = (ListView)findViewById(R.id.counterlist);
//		top_tbn_fanhui = (ImageButton)findViewById(R.id.nagivate_btn_left);
//        top_tbn_xinzeng = (Button)findViewById(R.id.navigate_btn_right);
//        top_tbn_xinzeng.setVisibility(Button.INVISIBLE);
//        title = (TextView)findViewById(R.id.title);
        
//        top_tbn_fanhui.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Intent counter = new Intent(CounterListActivity.this, HomeActivity.class);
//				startActivity(counter);
//				finish();
//			}
//		});
//        title.setText("计数");
		
		if(counterDataManager==null){
			counterDataManager= new CounterDataManager(this);
			counterDataManager.openDataBase();
		}
		
		Cursor cursor = counterDataManager.fetchAllCounterDatas();
		startManagingCursor(cursor);
		
		ListAdapter listAdapter = new CustomSca(this, R.layout.counterlist_item, 
                cursor,
                new String[]{"countname","countnum","counttoday","_id","begindate","enddate","_id"}, 
				new int[]{R.id.counterlist_item_digest,R.id.counterlist_item_typename,R.id.todaycountdata,R.id.hidenid,R.id.tv_begintime,R.id.tv_endtime,R.id.todaywc});
         
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				TextView t=(TextView)arg1.findViewById(R.id.hidenid);
				if(t!=null&&t.getText()!=null){
					Intent intent = new Intent(CounterListActivity.this, CounterActivity.class);
					intent.putExtra("id", t.getText());
					startActivity(intent);
					finish();
				}
				Log.v(TAG, "click:::"+t.getText());				
			}
    	});
	}


	@Override
	protected void onResume() {
		if (counterDataManager == null) {
			counterDataManager = new CounterDataManager(this);
			counterDataManager.openDataBase();
        }
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		 super.onDestroy();
		 if (counterDataManager  != null) {  
			 counterDataManager.closeDataBase(); 
			 counterDataManager = null;
		 } 
	}

	@Override
	protected void onPause() {
		if (counterDataManager != null) {
			counterDataManager.closeDataBase();
			counterDataManager = null;
        }
		super.onPause();
	}
	/**
	 * 捕捉back
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent counter1 = new Intent(CounterListActivity.this, HomeActivity.class);
			startActivity(counter1);
			finish();
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}

}
